﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace VarunRobinson.DocumentHelper_vsix
{
    class MyDocClass
    {
        public string Summary { get; set; }

        
        public List<string> Parameters { get; set; }

        private String _returns;

        public string Returns { 
            get
            {
                return _returns.Replace('<','{').Replace('>','}');
            } 
            set
            {
                _returns = value;
            }
        }

        /// <summary>
        /// DocumentFunction
        /// </summary>
        /// <returns>System.String</returns>
        public string DocumentFunction()
        {
            
            StringBuilder sb = new StringBuilder();
            sb.Append("<doc>");
            sb.Append("\r\n<summary>\r\n").Append(this.Summary.ToDisplayName()).Append("\r\n</summary>");
            foreach (var myparam in this.Parameters)
            {
                sb.Append("\r\n<param name=\"").Append(myparam).Append("\">").Append(myparam.ToDisplayName()).Append("</param>");
            }
            sb.Append("\r\n<returns>").Append("</returns>");
            sb.Append("\r\n</doc>");
            return sb.ToString();
        }



        public string DocumentClass()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("<doc>");
            sb.Append("\r\n<summary>\r\n").Append(this.Summary.ToDisplayName()).Append("\r\n</summary>");           
            sb.Append("\r\n</doc>");
            return sb.ToString();
        }
    }
    /// <summary>
    /// 
    /// </summary>
    public static class StringExtensions
    {
        /// <summary>
        /// ToSms
        /// </summary>
        /// <param name="source"></param>
        /// <param name="limit"></param>
        /// <returns>System.String</returns>
        public static string ToSms(this string source, int limit = 160)
        {
            return String.IsNullOrEmpty(source) ? String.Empty : source.Length > limit ? source.Substring(0, limit - 3).PadRight(limit, '.') : source;
        }

        /// <summary>
        /// ToDisplayName
        /// </summary>
        /// <param name="source"></param>
        /// <param name="stopwords"></param>
        /// <param name="keywords"></param>
        /// <returns>System.String</returns>
        public static string ToDisplayName(this string source, string[] stopwords = null, string[] keywords = null)
        {
            if (source == null) return null;

            // Uppercase followed by lowercase but not on existing word boundary (eg. the start)
            var camelCaseRegex = new Regex(@"(?<=\p{Ll})\p{Lu}", RegexOptions.Compiled);
            var keywordsRegex = new Regex(string.Concat(@"(?=\p{Lu})", string.Join("|", keywords)), RegexOptions.Compiled);
            var stopwordsRegex = new Regex(string.Format(@"\s*\b(?:{0})\b\s*", string.Join("|", stopwords)), RegexOptions.Compiled);

            // Wordify
            source = camelCaseRegex.Replace(source, " $0");
            // Wordify keywords
            source = keywordsRegex.Replace(source, "$0 ");
            // Remove stopwords
            source = stopwordsRegex.Replace(source, "");

            return source;
        }

        /// <summary>
        /// ToDisplayName
        /// </summary>
        /// <param name="source"></param>
        /// <returns>System.String</returns>
        public static string ToDisplayName(this string source)
        {
            return ToDisplayName(source, stopwords: new[] { "Id" }, keywords: new[] { "EY" });
        }

        /// <summary>
        /// Excerpt
        /// </summary>
        /// <param name="source"></param>
        /// <param name="q"></param>
        /// <param name="range"></param>
        /// <returns>System.String</returns>
        public static string Excerpt(this string source, string q, int range = 40)
        {
            const string prefix = "...";
            if (range < q.Length) range = q.Length;

            int index = source.IndexOf(q, StringComparison.InvariantCultureIgnoreCase);

            if (source.Length <= 2 * range)
            {
                return source;
            }
            else
            {
                int start = index - range < 0 ? 0 : index - range;
                int end = index + range > source.Length ? source.Length : index + range;
                int length = end - start;

                var result = source.Substring(start, length);
                if (start > 0)
                {
                    result = prefix + result;
                }
                if (source.Length > end)
                {
                    result = result + prefix;
                }
                return result;
            }
        }

        /// <summary>
        /// Highlight
        /// </summary>
        /// <param name="source"></param>
        /// <param name="q"></param>
        /// <returns>System.String</returns>
        public static string Highlight(this string source, string q)
        {
            return Regex.Replace(source, "(" + q + ")", "<strong>$1</strong>", RegexOptions.IgnoreCase);
        }

        /// <summary>
        /// Contains
        /// </summary>
        /// <param name="source"></param>
        /// <param name="keyword"></param>
        /// <param name="comparison"></param>
        /// <returns>System.Boolean</returns>
        public static bool Contains(this string source, string keyword, StringComparison comparison)
        {
            return source.IndexOf(keyword, comparison) >= 0;
        }
    }
}
